﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using QLKS2014.PUBLIC;
using System.Data;
using System.Windows.Forms;

namespace QLKS2014.DATA
{
    public class ChiTietPhieuThuePhongData
    {
        DataConnecter connect;
        
        public ChiTietPhieuThuePhongData()
        {
            connect = new DataConnecter();
        }

        //Select all ChiTietPhieuThuePhong
        //public DataTable selectAll()
        //{
        //    return connect.getData("ChiTietPhieuThuePhong_SelectAll");
        //}

        public List<ChiTietPhieuThuePhongPublic> selectAll()
        {
            try
            {
                DataTable dt = new DataTable();
                List<ChiTietPhieuThuePhongPublic> list = new List<ChiTietPhieuThuePhongPublic>();
                

                int row;
                dt = connect.getData("ChiTietPhieuThuePhong_SelectAll");
                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    ChiTietPhieuThuePhongPublic chiTietPhieuThuePhong = new ChiTietPhieuThuePhongPublic();
                    chiTietPhieuThuePhong.MaChiTietPhieuThuePhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    chiTietPhieuThuePhong.MaPhieuThue = int.Parse(dt.Rows[i].ItemArray[1].ToString());
                    chiTietPhieuThuePhong.TenKhachHang = dt.Rows[i].ItemArray[2].ToString();
                    chiTietPhieuThuePhong.CMND = dt.Rows[i].ItemArray[3].ToString();
                    chiTietPhieuThuePhong.DiaChi = dt.Rows[i].ItemArray[4].ToString();
                    chiTietPhieuThuePhong.MaLoaiKhachHang = int.Parse(dt.Rows[i].ItemArray[5].ToString());
                    chiTietPhieuThuePhong.TenLoaiKhachHang = dt.Rows[i].ItemArray[6].ToString();
                    list.Add(chiTietPhieuThuePhong);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

        //Thêm ChiTietPhieuThuePhong
        public void insert(ChiTietPhieuThuePhongPublic chiTietPhieuThuePhongPublic)
        {
            int param = 4;
            string[] name = new string[param];
            object[] values = new object[param];

            //name[0] = "@MaPhieuThue";
            name[0] = "@TenKhachHang";
            name[1] = "@CMND";
            name[2] = "@DiaChi";
            name[3] = "@MaLoaiKhachHang";

            //values[0] = chiTietPhieuThuePhongPublic.MaPhieuThue;
            values[0] = chiTietPhieuThuePhongPublic.TenKhachHang;
            values[1] = chiTietPhieuThuePhongPublic.CMND;
            values[2] = chiTietPhieuThuePhongPublic.DiaChi;
            values[3] = chiTietPhieuThuePhongPublic.MaLoaiKhachHang;

            connect.ExcuteNonQuery("ChiTietPhieuThuePhong_Insert", name, values, param);
        
        }

        //Select ChiTietPhieuThuePhong Tu MaPhieuThue
        public List<ChiTietPhieuThuePhongPublic> selectByMaPhieuThue(ChiTietPhieuThuePhongPublic chiTietPhieuThue)
        {
            try
            {
                DataTable dt = new DataTable();
                List<ChiTietPhieuThuePhongPublic> list = new List<ChiTietPhieuThuePhongPublic>();
                int param = 1;
                string[] name = new string[param];
                object[] values = new object[param];

                name[0] = "@MaPhieuThue";
                values[0] = chiTietPhieuThue.MaPhieuThue;

                int row;
                dt = connect.getData("ChiTietPhieuThuePhong_SelectByMaPhieuThue",name,values,param);

                row = dt.Rows.Count;
                for (int i = 0; i < row; i++)
                {
                    ChiTietPhieuThuePhongPublic chiTietPhieuThuePhong = new ChiTietPhieuThuePhongPublic();
                    chiTietPhieuThuePhong.MaChiTietPhieuThuePhong = int.Parse(dt.Rows[i].ItemArray[0].ToString());
                    chiTietPhieuThuePhong.MaPhieuThue = int.Parse(dt.Rows[i].ItemArray[1].ToString());
                    chiTietPhieuThuePhong.TenKhachHang = dt.Rows[i].ItemArray[2].ToString();
                    chiTietPhieuThuePhong.CMND = dt.Rows[i].ItemArray[3].ToString();
                    chiTietPhieuThuePhong.DiaChi = dt.Rows[i].ItemArray[4].ToString();
                    chiTietPhieuThuePhong.MaLoaiKhachHang = int.Parse(dt.Rows[i].ItemArray[5].ToString());
                    list.Add(chiTietPhieuThuePhong);
                }
                return list;
            }
            catch (Exception ex)
            {
                //bao loi
                MessageBox.Show(ex.Message, "Không Thể Load Dữ Liệu");
                return null;
            }
        }

    }
}